//
//  SINACourceScroViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2017/12/21.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "SINACourseSignViewController.h"
#import "SINADisciplineResultModel.h"
#import "SINACourseResultsTableViewCell.h"
#import "SINAStuSchoolYearModel.h"
#import "SINAStuSchoolYearScoreModel.h"

@interface SINACourseSignViewController ()<UITableViewDelegate,UITableViewDataSource>
//学年
@property (weak, nonatomic) IBOutlet UILabel *studyYearTieleLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong) NSMutableArray *dataList;
@property(nonatomic,strong) NSMutableArray *schoolYearDataList;

//暂无学科成绩
@property (weak, nonatomic) IBOutlet UILabel *noClassScoreLabel;
@property (weak, nonatomic) IBOutlet UILabel *noClassScoreAndYearLabel;
@property(nonatomic,assign) NSInteger currentSchoolIndex;


@end

@implementation SINACourseSignViewController
//上一学年
- (IBAction)toUpYearButtonClick:(UIButton *)sender {
    self.currentSchoolIndex--;
    if(self.currentSchoolIndex<=0){
        self.currentSchoolIndex=0;
    }
    [self showSchoolYear:self.currentSchoolIndex];
}

//下一学年
- (IBAction)toNextYearButtonClick:(UIButton *)sender {
    self.currentSchoolIndex++;
    if(self.currentSchoolIndex>=self.schoolYearDataList.count){
        self.currentSchoolIndex=self.schoolYearDataList.count-1;
    }
    [self showSchoolYear:self.currentSchoolIndex];
}

- (void)viewDidLoad {
    [super viewDidLoad];
   self.navigationItem.title=@"课程签到";
    [self.view setBackgroundColor:[UIColor colorWithHexString:@"#eeeeee"]];
    
    [self.studyYearTieleLabel setTextColor: [UIColor colorWithRed:0/255 green:198.0/255 blue:174.0/255 alpha:0.8f]];
    
    self.schoolYearDataList = [[NSMutableArray alloc]init];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.backgroundColor=[UIColor colorWithHexString:@"#eeeeee"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    for (int i=0; i<0; i++) {
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:
                           @"运行生理学", @"courseTitle",
                           @"1", @"totalTime",
                           @"20", @"currentTime",
                           nil];
        SINADisciplineResultModel *item = [[SINADisciplineResultModel alloc]initWithDict:dic];
        [self.dataList addObject:item];
    }
    [self.tableView reloadData];
    // Do any additional setup after loading the view from its nib.
    
    [self loadingSchoolYearListNetData];
}

-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(NSMutableArray *)dataList{
    if (!_dataList) {
        _dataList = [NSMutableArray array];
    }
    return  _dataList;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SINACourseResultsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SINACourseResultsTableViewCell"];
    
    if (cell == nil) {
        cell= [[[NSBundle mainBundle]loadNibNamed:@"SINACourseResultsTableViewCell" owner:nil options:nil] firstObject];
        
    }
    SINAStuSchoolYearScoreModel *model=self.dataList[indexPath.row];
    cell.flagIndex=1;
    cell.scoreModel = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell ;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}
//查看详情
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
//    NSInteger index = indexPath.row;

}

/**
 *分页加载数据
 */
-(void)loadingNetData:(NSInteger) pageIndex pageSize:(NSInteger) pageSize user:(NSString*) userId{
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setObject:[NSString stringWithFormat:@"%ld",(long)pageIndex] forKey:@"pageIndex"];
    [dict setObject:[NSString stringWithFormat:@"%ld",(long)pageIndex] forKey:@"pageSize"];
    [dict setObject:[NSString stringWithFormat:@"%@",userId] forKey:@"userId"];
    
    [CXAFNHttpRequestUtils httpPostRequest:[NSString stringWithFormat:@"%@%@",baseUrl,userLoginUrl] withParamters:dict success:^(id responseData) {
        
        NSString *code = responseData[@"code"];
        
        if ([code isEqualToString:@"1000"]) {
//            NSDictionary *result = responseData[@"content"];
            
        }else{
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
        }
        
        
        
    } failure:^(NSError *error, NSInteger errCode) {
        DLog(@"加载 失败  %@ ",error.debugDescription);
        
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"加载数据失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
    }];
}

//获取学年信息
-(void)loadingSchoolYearListNetData{
    
    [self startLoading];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    //用户的uuid String uuid

    [dict setObject:[[SINACommonUserHelper share]getUserModel].userUid forKey:@"uuid"];
    
    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@",baseUrl,stuGetSchoolYearListUrl] withParamters:dict success:^(id responseData) {
        [weakSelf stopLoading];
        NSString *code = responseData[@"code"];
        
        if ([code isEqualToString:@"1000"]) {
            NSArray *result = responseData[@"content"];
            if (result==nil||result.count==0) {
                [self showNoDataAndNoSchool];
            } else {
                //有学年信息
                for(NSInteger i=0;i<result.count;i++){
                    NSDictionary *dic = result[i];
                    SINAStuSchoolYearModel *model = [[SINAStuSchoolYearModel alloc] initWithDict:dic];
                    [self.schoolYearDataList addObject:model];
                }
                [self showSchoolYear:0];
            }
            
        }else{
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
            [self showNoDataAndNoSchool];
        }
        
        
        
    } failure:^(NSError *error, NSInteger errCode) {
        DLog(@"加载 失败  %@ ",error.debugDescription);
        [weakSelf stopLoading];
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"加载数据失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
        [self showNoDataAndNoSchool];
    }];
}

//无学年无数据页面
-(void) showNoDataAndNoSchool{
    self.noClassScoreAndYearLabel.hidden=NO;
    self.noClassScoreLabel.hidden=YES;
}
//有学年无数据页面
-(void) showNoDataAndSchool{
    self.noClassScoreAndYearLabel.hidden=YES;
    self.noClassScoreLabel.hidden=NO;
}

-(void)showSchoolYear:(NSInteger) yearArae{
    if(yearArae<self.schoolYearDataList.count){
        SINAStuSchoolYearModel *model = self.schoolYearDataList[yearArae];
        //设置数据
        self.currentSchoolIndex = yearArae;
        self.studyYearTieleLabel.text=[NSString stringWithFormat:@"%@ %@",model.schoolyear,model.term];
    
        [self loadingSchoolYearScoreListNetData ];
    }
}

//获取学年成绩信息
-(void)loadingSchoolYearScoreListNetData{
    
    SINAStuSchoolYearModel *model = self.schoolYearDataList[self.currentSchoolIndex];
    
    [self startLoading];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    //用户的uuid String uuid
    
    [dict setObject:[[SINACommonUserHelper share]getUserModel].userUid forKey:@"useruuid"];
    
    [dict setObject:model.schoolyear forKey:@"schoolyear"];
    
    [dict setObject:model.term forKey:@"term"];
    
    //String useruuid
    //学年 String schoolyear
    //学期 String term

    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@",baseUrl,stuGetSchoolYearSignListUrl] withParamters:dict success:^(id responseData) {
        [weakSelf stopLoading];
        NSString *code = responseData[@"code"];
        
        if ([code isEqualToString:@"1000"]) {
            NSArray *result = responseData[@"content"];
            if (result==nil||result.count==0) {
                [self showNoDataAndSchool];
            } else {
                [self.dataList removeAllObjects];
                //有成绩信息
                for(NSInteger i=0;i<result.count;i++){
                    NSDictionary *dic = result[i];
                    SINAStuSchoolYearScoreModel *model = [[SINAStuSchoolYearScoreModel alloc] initWithDict:dic];
                    [self.dataList addObject:model];
                }
                [self.tableView reloadData];
            }
            
        }else{
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
            [self showNoDataAndSchool];
        }
        
        
        
    } failure:^(NSError *error, NSInteger errCode) {
        DLog(@"加载 失败  %@ ",error.debugDescription);
        [weakSelf stopLoading];
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"加载数据失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
        [self showNoDataAndSchool];
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
